package com.mashibing.serviceverificationcode.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.response.NumberCodeResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

@RestController
public class ServiceVerificationCodeController {

    @GetMapping("/numberCode/{size}")
    @SentinelResource("numberCode")
    public ResponseResult numberCode(@PathVariable int size, HttpServletRequest request) {

        String tag = request.getHeader("tag");
        System.out.println("请求头tag=" + tag);

        // 生成字符串验证码
        String stringChar = "abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ";
        char[] stringCharArray = stringChar.toCharArray();
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < size; i++) {
            builder.append(stringCharArray[new Random().nextInt(52)]);
        }

        System.out.println("验证码为" + builder);
        NumberCodeResponse numberCodeResponse = new NumberCodeResponse();
        numberCodeResponse.setNumberCode(builder.toString());

        return ResponseResult.success(numberCodeResponse);
    }
}
